﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class Portfolio : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            petacti_sliki_vo_lista((string)Session["username"]);
        }
    }
    
    protected void btn_show_Click(object sender, EventArgs e)
    {
         

        ////prikazuva slika golema--------------------

        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = @"Data Source=localhost\SQLEXPRESS;" + "Initial Catalog=Photographers;Integrated Security=SSPI";

        string sqlQuestion2 = "SELECT * FROM Photos WHERE ID=@ID";

        SqlCommand komanda2 = new SqlCommand(sqlQuestion2, myConnection);

        komanda2.Parameters.AddWithValue("@ID", ListBox1.SelectedItem.Value.ToString());
        SqlDataReader citac2;
        try
        {
            myConnection.Open();
            citac2 = komanda2.ExecuteReader();
            while (citac2.Read())
            {
                string ime = citac2["image"].ToString();
                Response.Redirect("PrikazSlika.apsx?id=" + ime);
            }
        }
        catch (Exception err)
        {    
            myConnection.Close();
        }
        finally
        {
            myConnection.Close();
        }
    }
    protected void btn_remove_Click(object sender, EventArgs e)
    {
        int i = Convert.ToInt32(ListBox1.SelectedValue);

        brisi_komentari(i);
        brisi_slika(i);

        lbl_port_points.Text = "";
        lbl_port_votes.Text = "";
        ImageButton1.Enabled = false;
        btn_remove.Visible = false;
        petacti_sliki_vo_lista((string)Session["username"]);
    }
    protected void petacti_sliki_vo_lista(string username)
    {
        ListBox1.Items.Clear();
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["Konekcija"].ConnectionString;
        string sqlQuestion2 = "SELECT * FROM Photos WHERE username=@username";

        SqlCommand komanda2 = new SqlCommand(sqlQuestion2, myConnection);
        komanda2.Parameters.AddWithValue("@username", username);
        
        SqlDataReader citac2;
        try
        {
            myConnection.Open();
            citac2 = komanda2.ExecuteReader();
            while (citac2.Read())
            {
                ListItem item = new ListItem();
                item.Text = citac2["name"].ToString();
                item.Value = citac2["ID"].ToString();
                ListBox1.Items.Add(item);
            }
        }
        catch (Exception err)
        {
            myConnection.Close();
        }
        finally
        {
            myConnection.Close();
        }
        ListBox1.SelectedIndex = -1;
        ImageButton1.Enabled = false;
    }
    protected void brisi_slika(int ID)
    {
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["Konekcija"].ConnectionString;
        string sqlQuestion2 = "DELETE FROM Photos WHERE ID=@ID";
        SqlCommand komanda2 = new SqlCommand(sqlQuestion2, myConnection);
        komanda2.Parameters.AddWithValue("@ID", ListBox1.SelectedItem.Value.ToString());
        try
        {
            myConnection.Open();
            komanda2.ExecuteNonQuery();
        }
        catch (Exception err)
        {
            Label1.Text = err.Message;
        }
        finally
        {
            myConnection.Close();
        }

    }

    protected void LinkButton7_Click(object sender, EventArgs e)
    {
        Response.Redirect("Upload.aspx");
    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = @"Data Source=localhost\SQLEXPRESS;" + "Initial Catalog=Photographers;Integrated Security=SSPI";

        string sqlQuestion2 = "SELECT * FROM Photos WHERE ID=@ID";

        SqlCommand komanda2 = new SqlCommand(sqlQuestion2, myConnection);

        komanda2.Parameters.AddWithValue("@ID", ListBox1.SelectedItem.Value.ToString());
        int indikator = 0;
        SqlDataReader citac2;
        try
        {
            myConnection.Open();
            citac2 = komanda2.ExecuteReader();
            while (citac2.Read())
            {

                ImageButton1.ImageUrl = citac2["image"].ToString();
                ImageButton1.BorderWidth = 3;
                lbl_port_points.Text = citac2["points"].ToString() + " points  ";
                lbl_port_votes.Text = citac2["votes"].ToString() + " votes";
                ImageButton1.Enabled = true;
                btn_remove.Visible = true;
            }

        }
        catch (Exception err)
        {
            Label1.Text += "error reading comentari ";
            Label1.Text += err.Message;
            myConnection.Close();
        }
        finally
        {
            myConnection.Close();
        }   
    }

    protected void brisi_komentari(int ID)
    {
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = @"Data Source=localhost\SQLEXPRESS;" + "Initial Catalog=Photographers;Integrated Security=SSPI";
        string sqlQuestion2 = "DELETE FROM Comment WHERE ID=@ID";
        SqlCommand komanda2 = new SqlCommand(sqlQuestion2, myConnection);
        komanda2.Parameters.AddWithValue("@ID", ID);
        try
        {
            myConnection.Open();
            komanda2.ExecuteNonQuery();
        }
        catch (Exception err)
        {
            Label1.Text = "Error deleting comments. ";
            Label1.Text += err.Message;
        }
        finally
        {
            myConnection.Close();
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {

      string i= ListBox1.SelectedItem.Value.ToString();

                Response.Redirect("PrikazSlika.aspx?id=" + i);


    }
}
